package com.maxh.controller;

import com.maxh.annotation.PassToken;
import com.maxh.annotation.SuperAuthority;
import com.maxh.annotation.AdminLoginToken;
import com.maxh.pojo.Push;
import com.maxh.service.PushService;
import com.maxh.util.ReturnData;
import com.maxh.util.ReturnError;
import com.maxh.util.ReturnUtil;
import com.maxh.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class PushController {
    @Autowired
    PushService pushService;

    /*管理员获取所有通知信息*/
    @AdminLoginToken
    @GetMapping("/training/push/notice/list/all")
    public Object getNoticeList(){
        List noticeList =  pushService.getNoticeList();
        System.out.println("管理员获取所有通知信息："+noticeList);
        if(noticeList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,noticeList ,"后端数据交互正常！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,noticeList,"数据为空");
        }
    }

    //管理员修改通知发布状态
    @AdminLoginToken
    @PostMapping("/training/push/notice/updateUseStatus")
    public Object updateNoticeStatus(Push push) throws ParseException {
        if(push.getUseStatus()+""==null) return new ReturnError(StatusCode.RETURN_ERROR,"通知状态不能为空");
        Date createTime = new ReturnUtil().dateTime();
        push.setCreateTime(createTime);
        Boolean isUpdateCourseStatus = pushService.updateUseStatus(push);
        System.out.println("管理员修改通知发布状态："+isUpdateCourseStatus);
        if(isUpdateCourseStatus){
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,"使用状态修改成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"使用状态修改失败！");
        }
    }

    /*超级管理员删除公告信息*/
    @SuperAuthority
    @PostMapping("/training/push/notice/delete")
    public Object deleteNotice(@RequestParam Map<String,Object> map){
        System.out.println(map.get("id"));
        if(map.get("id")==null)  return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");
        Boolean isDelete = pushService.deleteNotice(map.get("id")+"");
        if(isDelete){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,"删除通知成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"删除通知失败");
        }
    }

    // 管理员修改通知信息
    @AdminLoginToken
    @PostMapping("/training/push/notice/update")
    public Object updateNotice(Push push) throws ParseException {
        if(push.getId()+""==null)  return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");
        Date createTime = new ReturnUtil().dateTime();
        push.setCreateTime(createTime);
        Boolean isUpdateCourse = pushService.updateNoticeList(push);
        System.out.println("管理员修改通知信息："+isUpdateCourse);
        if(isUpdateCourse){
            push = pushService.searchNoticeById(push);
            return new ReturnData(StatusCode.REQUEST_SUCCESS , push,"修改成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"修改失败！");
        }
    }

    //  管理员添加通知信息
    @AdminLoginToken
    @PostMapping("/training/push/notice/add")
    public Object addNotice(Push push) throws ParseException {
        if(push.getTitle()==null || push.getDetail()==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"必填信息不能为空");
        }
        Date createTime = new ReturnUtil().dateTime();
        push.setCreateTime(createTime);
        push.setStatus(1);
        push.setPushType(1);
        Boolean  isAddCourse = pushService.addNoticeList(push);
        System.out.println("管理员添加通知信息："+isAddCourse);
        if(isAddCourse){
            push = pushService.searchNoticeById(push);
            return new ReturnData(StatusCode.REQUEST_SUCCESS, push,"添加成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"添加失败！");
        }
    }

    /*管理员获取所有公告信息*/
    @AdminLoginToken
    @GetMapping("/training/push/article/list/all")
    public Object getArticleList(){
        List articleList =  pushService.getArticleList();
        System.out.println("管理员获取所有公告信息："+articleList);
        if(articleList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,articleList ,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,articleList ,"数据为空！");
        }
    }

    /*超级管理员删除文章信息*/
    @SuperAuthority
    @PostMapping("/training/push/article/delete")
    public Object deleteArticle(@RequestParam Map<String,Object> map){
        System.out.println(map.get("id"));
        if(map.get("id")==null)  return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");
        Boolean isDelete = pushService.deleteNotice(map.get("id")+"");
        if(isDelete){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,"删除文章成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"删除文章失败");
        }
    }

    //管理员修改文章使用状态
    @AdminLoginToken
    @PostMapping("/training/push/article/updateUseStatus")
    public Object updateArticleStatus(Push push) throws ParseException {
        if(push.getUseStatus()+""==null) return new ReturnError(StatusCode.RETURN_ERROR,"文章使用状态不能为空");
        Date createTime = new ReturnUtil().dateTime();
        push.setCreateTime(createTime);
        Boolean isUpdateCourseStatus = pushService.updateUseStatus(push);
        System.out.println("管理员修改文章使用状态："+isUpdateCourseStatus);
        if(isUpdateCourseStatus){
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,"修改成功！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"修改失败！");
        }
    }

    //  修改文章信息
    @SuperAuthority
    @PostMapping("/training/push/article/update")
    public Object updateArticle(Push push) throws ParseException {
        if(push.getId()+""==null)  return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");
        Date createTime = new ReturnUtil().dateTime();
        push.setCreateTime(createTime);
        Boolean isUpdateCourse = pushService.updateNoticeList(push);
        System.out.println("修改文章信息："+isUpdateCourse);
        if(isUpdateCourse){
            push = pushService.searchNoticeById(push);
            return new ReturnData(StatusCode.REQUEST_SUCCESS , push,"后端数据交互正常！");
        }else{
            return new ReturnData(StatusCode.RETURN_ERROR ,"修改失败！");
        }
    }

    //  管理员添加文章信息
    @AdminLoginToken
    @PostMapping("/training/push/article/add")
    public Object addArticle(Push push) throws ParseException {
        if(push.getTitle()==null || push.getDetail()==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"必填信息不能为空");
        }
        Date createTime = new ReturnUtil().dateTime();
        push.setCreateTime(createTime);
        push.setStatus(1);
        push.setPushType(2);
        Boolean  isAddCourse = pushService.addNoticeList(push);
        System.out.println("管理员添加文章信息："+isAddCourse);
        if(isAddCourse){
            push = pushService.searchNoticeById(push);
            return new ReturnData(StatusCode.REQUEST_SUCCESS, push,"添加成功！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,"添加失败！");
        }
    }

    /*学员获取已发布通知信息*/
    @PassToken
    @GetMapping("/training/push/notice/list")
    public Object getNoticeListByStatus(){
        List noticeList =  pushService.getNoticeListByStatus();
        System.out.println("学员获取已发布通知信息："+noticeList);
        if(noticeList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,noticeList ,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,noticeList,"数据为空");
        }
    }

    /*学员获取已发布文章信息*/
    @PassToken
    @GetMapping("/training/student/push/article/list")
    public Object getArticleListByUser(){
        List noticeList =  pushService.getArticleListByUser();
        System.out.println("学员获取已发布文章信息："+noticeList);
        if(noticeList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,noticeList ,"数据获取完成！");
        }else{
            return new ReturnData(StatusCode.REQUEST_SUCCESS,noticeList,"数据为空");
        }
    }
    /*学员根据id获取已发布文章信息*/
    @PassToken
    @GetMapping("/training/push/detail/byId")
    public Object getPushById(Push push){
        if(push.getId()+""==null)  return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");
        push = pushService.searchNoticeById(push);
        System.out.println("学员根据id获取已发布文章信息："+push);
        return new ReturnData(StatusCode.REQUEST_SUCCESS,push ,"数据获取完成！");
    }
}
